<HTML>
<!--
  -- Copyright (c) 1996-1999
  -- Silicon Graphics Computer Systems, Inc.
  --
  -- Permission to use, copy, modify, distribute and sell this software
  -- and its documentation for any purpose is hereby granted without fee,
  -- provided that the above copyright notice appears in all copies and
  -- that both that copyright notice and this permission notice appear
  -- in supporting documentation.  Silicon Graphics makes no
  -- representations about the suitability of this software for any
  -- purpose.  It is provided "as is" without express or implied warranty.
  --
  -- Copyright (c) 1994
  -- Hewlett-Packard Company
  --
  -- Permission to use, copy, modify, distribute and sell this software
  -- and its documentation for any purpose is hereby granted without fee,
  -- provided that the above copyright notice appears in all copies and
  -- that both that copyright notice and this permission notice appear
  -- in supporting documentation.  Hewlett-Packard Company makes no
  -- representations about the suitability of this software for any
  -- purpose.  It is provided "as is" without express or implied warranty.
  --
  -->
<Head>
<Title>char_producer</Title>
<!-- Generated by htmldoc -->
</HEAD>
<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" 
	ALINK="#ff0000"> 
<IMG SRC="CorpID.gif" 
     ALT="SGI" HEIGHT="43" WIDTH="151"> 
<!--end header-->
<BR Clear>
<H1>char_producer</H1>

<Table CellPadding=0 CellSpacing=0 width=100%>
<TR>
<TD Align=left><Img src = "containers.gif" Alt=""   WIDTH = "194"  HEIGHT = "38" ></TD>
<TD Align=right><Img src = "type.gif" Alt=""   WIDTH = "194"  HEIGHT = "39" ></TD>
</TR>
<TR>
<TD Align=left VAlign=top><b>Category</b>: containers</TD>
<TD Align=right VAlign=top><b>Component type</b>: type</TD>
</TR>
</Table>

<h3>Description</h3>
<tt>Char_producter&lt;charT&gt;</tt> is a helper class for <tt><A href="Rope.html">rope</A></tt>.  Its only
purpose is for the creation of <tt>rope</tt>s that use lazy evaluation.  A
<tt>rope</tt> that uses lazy evaluation is one that does not store
the value of the <tt>n</tt>th character until the value of that particular
character is actually needed.  This is sometimes a useful 
optimization.
<P>
<tt>Char_producer&lt;charT&gt;</tt> is an abstract base class; it provides an
empty virtual destructor and a pure virtual <tt>operator()</tt>.  Only 
subclasses of <tt>char_producer</tt> can be used, not <tt>char_producer</tt>
directly.
<P>
Classes derived from <tt>char_producer</tt> define a method for computing the
characters in a <tt><A href="Rope.html">rope</A></tt> lazily, whenever those characters are
needed.  Unlike <A href="functors.html">Function Objects</A>, <tt>char_producer</tt>s can be stored
inside a <tt><A href="Rope.html">rope</A></tt> data structure.  All char producers must be derived
from the single base class <tt>char_producer</tt>.
<P>
For performance reasons, the <tt>operator()</tt> inside <tt>char_producer</tt> is
invoked to fill a buffer with a sequence of characters 
rather than producing a single character at a time.  Its declaration
is
<pre>
  virtual void operator()(size_t start_pos, size_t len, charT* buffer) = 0;
</pre>
<P>
An invocation of <tt>operator()</tt> requests that <tt>len</tt> characters starting
at <tt>start_pos</tt> should be deposited into <tt>buffer</tt>.  If
the character at the <tt>n</tt>th position is requested twice, the same
character must be returned both times. (That is, a <tt>char_producer</tt>
must represent a specific, deterministic sequence of characters.)
<P>
The following is an example of how to use <tt>char_producer</tt> and lazy
evaluation: it is a (somewhat naive implementation of a)
<tt>char_producer</tt> subclass, which allows an entire file to be treated as
a <tt><A href="Rope.html">rope</A></tt>.  A <tt>rope</tt> constructed from <tt>file_char_prod</tt> will contain the
same character sequence as the file specified in the constructor.  The
file will be read only when the <tt>rope</tt> is accessed, not when the
<tt>rope</tt> is constructed.
<pre>
void fail(char* s) { 
  fprintf(stderr, &quot;%s errno = %d\n&quot;, s, errno); 
  exit(1); 
}

class file_char_prod : public char_producer&lt;char&gt; {
  public:
    FILE* f;
    file_char_prod(char *file_name) {
      if (NULL == (f = fopen(file_name, &quot;rb&quot;))) 
        fail(&quot;Open failed&quot;);
    }

    ~file_char_prod() { fclose(f); }

    virtual void operator()(size_t start_pos, size_t len, char* buffer) {
      if (fseek(f, start_pos, SEEK_SET)) fail(&quot;Seek failed&quot;);
      if (fread(buffer, sizeof(char), len, f) &lt; len) fail(&quot;Read failed&quot;);
    }

    long len() {
        // Return the length of a file; this is the only
        //   mechanism that the standard C library makes possible.
      if (fseek(f, 0, SEEK_END)) fail(&quot;Seek failed&quot;);
      return ftell(f);
    }
};
</pre>
<P>
The following program uses the above class to extract and write the
middle 200 characters of a file.  Note that even if it is invoked on a
gigabyte file, it will still only read approximately 200 characters
from the file.  Likewise, the <tt>rope</tt> will only require a small amount of
memory.
<pre>
int main(int argc, char** argv)
{
  if (argc != 2) 
    fail(&quot;wrong number of arguments&quot;);

  file_char_prod* fcp = new file_char_prod(argv[1]);
  <A href="Rope.html">crope</A> s(fcp, fcp -&gt; len(), true);
  size_t len = s.size();
  <A href="Rope.html">crope</A> middle = s.substr(len/2 - 100, 200) + &quot;\n&quot;;
  fwrite(middle.c_str(), sizeof(char), middle.size(), stdout);
}
</pre>
<h3>Definition</h3>
Defined in <A href="rope">rope</A>, and in the backward-compatibility header <A href="rope.h">rope.h</A>.
The <tt>char_producer</tt> class is an SGI extension; it is not part of the C++
standard.
<h3>Template parameters</h3>
<Table border>
<TR>
<TH>
Parameter
</TH>
<TH>
Description
</TH>
<TH>
Default
</TH>
</TR>
<TR>
<TD VAlign=top>
<tt>charT</tt>
</TD>
<TD VAlign=top>
The character type
</TD>
<TD VAlign=top>
&nbsp;
</TD>
</tr>
</table>
<h3>Model of</h3>
<A href="Assignable.html">Assignable</A>
<h3>Type requirements</h3>
<tt>charT</tt> is a model of <A href="Assignable.html">Assignable</A>.
<h3>Members</h3>
<Table border>
<TR>
<TH>
Member
</TH>
<TH>
Where defined
</TH>
<TH>
Description
</TH>
</TR>
<TR>
<TD VAlign=top>
<tt>virtual ~char_producer()</tt>
</TD>
<TD VAlign=top>
<tt>char_producer</tt>
</TD>
<TD VAlign=top>
A virtual destructor.
</TD>
</TR>
<TR>
<TD VAlign=top>
&nbsp;
</TD>
<TD VAlign=top>
<pre>
virtual void operator()(size_t start_pos, 
                        size_t len,
                        charT* buffer)
</pre>
</TD>
<TD VAlign=top>
Copy <tt>len</tt> characters starting at position <tt>start_pos</tt> in the
    string into <tt>buffer</tt>.  If a character at a particular
    position is requested more than once, each request must result in
    the same character.  Note that this is a pure virtual function; it
    must be overridden by every subclass of <tt>char_producer</tt>.
</TD>
</tr>
</table>
<h3>Notes</h3>
<h3>See also</h3>
<tt><A href="Rope.html">rope</A></tt>, <A href="functors.html">Function object</A>

<!--start footer--> 
<HR SIZE="6">
<A href="http://www.sgi.com/"><IMG SRC="surf.gif" HEIGHT="54" WIDTH="54" 
        ALT="[Silicon Surf]"></A>
<A HREF="index.html"><IMG SRC="stl_home.gif" 
        HEIGHT="54" WIDTH="54" ALT="[STL Home]"></A>
<BR>
<FONT SIZE="-2">
<A href="http://www.sgi.com/Misc/sgi_info.html" TARGET="_top">Copyright &copy; 
1999 Silicon Graphics, Inc.</A> All Rights Reserved.</FONT>
<FONT SIZE="-3"><a href="http://www.sgi.com/Misc/external.list.html" TARGET="_top">TrademarkInformation</A>
</FONT>
<P>
</BODY>
</HTML> 
